# coding: utf-8
class Codigo():
    def __init__(self,classes):
        self.classes = classes
        self.handle_noargs()


    def handle_noargs(self):
        try:
            for line in self.handle_inspection():
                print line
        except NotImplementedError:
            raise CommandError("Database inspection isn't supported for the currently selected database backend.")

    def handle_inspection(self):
        import keyword


        yield "# coding: utf-8"
        yield "# Este é um modulo de auto geração de classes django."
        yield ''
        yield 'from django.db import models'
        yield ''
        for cl in self.classes.lista:
            if cl.herda_de:
                yield 'class %s(%s):' % (cl.nome,cl.herda_de)
            else:
                yield 'class %s(models.Model):' % cl.nome
            for att in cl.atributos:
                extra = ""
                if len(att.extras):
                    for ex in att.extras:
                        extra+="%s=%s," %(ex['nome'],ex['valor'])
                yield '    %s = models.%s(%s)' % (att.nome, att.valor,extra[:-1])
            for rel in cl.associacoes:
                extra = ""

                if len(rel.extras):
                    for ex in rel.extras:
                        extra+=", %s='%s'" %(ex['nome'],ex['valor'])
                if rel.cardinalidade[0] == '*' and rel.cardinalidade[1] == '*':
                    yield '    %s = models.ManyToManyField(%s%s)' % (str.lower(str(rel.classe)), rel.classe, extra)
                else:
                    yield '    %s = models.ForeignKey(%s%s)' % (str.lower(str(rel.classe)), rel.classe, extra)


            yield ''
        """
            try:
                relations = connection.introspection.get_relations(cursor, table_name)
            except NotImplementedError:
                relations = {}
            try:
                indexes = connection.introspection.get_indexes(cursor, table_name)
            except NotImplementedError:
                indexes = {}
            for i, row in enumerate(connection.introspection.get_table_description(cursor, table_name)):
                att_name = row[0].lower()
                comment_notes = [] # Holds Field notes, to be displayed in a Python comment.
                extra_params = {}  # Holds Field parameters such as 'db_column'.

                # If we need to do field name modifiations, 
                # remember the original field name
                if ' ' in att_name or '-' in att_name or keyword.iskeyword(att_name):
                    extra_params['db_column'] = att_name
                  
                # Now modify the field name to make it python compatible.  
                if ' ' in att_name:
                    att_name = att_name.replace(' ', '_')
                    comment_notes.append('Field renamed to remove spaces.')
                if '-' in att_name:
                    att_name = att_name.replace('-', '_')
                    comment_notes.append('Field renamed to remove dashes.')
                if keyword.iskeyword(att_name):
                    att_name += '_field'
                    comment_notes.append('Field renamed because it was a Python reserved word.')

                if i in relations:
                    rel_to = relations[i][1] == table_name and "'self'" or table2model(relations[i][1])
                    field_type = 'ForeignKey(%s' % rel_to
                    if att_name.endswith('_id'):
                        att_name = att_name[:-3]
                    else:
                        extra_params['db_column'] = att_name
                else:
                    try:
                        field_type = connection.introspection.data_types_reverse[row[1]]
                    except KeyError:
                        field_type = 'TextField'
                        comment_notes.append('This field type is a guess.')

                    # This is a hook for DATA_TYPES_REVERSE to return a tuple of
                    # (field_type, extra_params_dict).
                    if type(field_type) is tuple:
                        field_type, new_params = field_type
                        extra_params.update(new_params)

                    # Add max_length for all CharFields.
                    if field_type == 'CharField' and row[3]:
                        extra_params['max_length'] = row[3]

                    if field_type == 'DecimalField':
                        extra_params['max_digits'] = row[4]
                        extra_params['decimal_places'] = row[5]

                    # Add primary_key and unique, if necessary.
                    column_name = extra_params.get('db_column', att_name)
                    if column_name in indexes:
                        if indexes[column_name]['primary_key']:
                            extra_params['primary_key'] = True
                        elif indexes[column_name]['unique']:
                            extra_params['unique'] = True

                    field_type += '('

                # Don't output 'id = meta.AutoField(primary_key=True)', because
                # that's assumed if it doesn't exist.
                if att_name == 'id' and field_type == 'AutoField(' and extra_params == {'primary_key': True}:
                    continue

                # Add 'null' and 'blank', if the 'null_ok' flag was present in the
                # table description.
                if row[6]: # If it's NULL...
                    extra_params['blank'] = True
                    if not field_type in ('TextField(', 'CharField('):
                        extra_params['null'] = True

                field_desc = '%s = models.%s' % (att_name, field_type)
                if extra_params:
                    if not field_desc.endswith('('):
                        field_desc += ', '
                    field_desc += ', '.join(['%s=%r' % (k, v) for k, v in extra_params.items()])
                field_desc += ')'
                if comment_notes:
                    field_desc += ' # ' + ' '.join(comment_notes)
                yield '    %s' % field_desc
            yield '    class Meta:'
            yield '        db_table = %r' % table_name
            yield ''
        """
